package com.czp.job.service.impl;

import java.sql.Timestamp;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.ibatis.session.RowBounds;

import com.czp.framework.model.DataStoreTemplate;
import com.czp.framework.util.DateUtils;
import com.czp.job.mapper.IJobMapper;
import com.czp.job.model.Job;
import com.czp.job.model.RelationJob;
import com.czp.job.model.RelationPerson;
import com.czp.job.service.IJobService;
import com.google.gson.Gson;

/**
 * 【环 境】：J2SE 1.7
 * @author   柴志鹏	CHAIZP@GMAIL.COM
 * @version  version 1.0
 * @since    2014-7-17
 */
public class JobServiceImpl implements IJobService {
	private IJobMapper jobMapper;
	private Gson gson = new Gson();
	
	public String queryJobList(Job job) throws Exception {
		RowBounds bounds = new RowBounds(job.getStart(), job.getLimit());
		DataStoreTemplate dst = new DataStoreTemplate();
		dst.setTotal(jobMapper.getJobRecords(job));
		dst.setList(jobMapper.queryJobList(job, bounds));
		return gson.toJson(dst);
	}
	
	public int jobController(Job job) throws Exception {
		Timestamp time = DateUtils.getSysTimestamp();
		job.setModifyDate(time);
		if(job.getCudType().equals("C")) {//添加
			job.setCreateDate(time);
			return jobMapper.insertJob(job);
		} else if(job.getCudType().equals("U")) {//修改
			return jobMapper.updateJob(job);
		} else if(job.getCudType().equals("D")) {//删除
			return jobMapper.deleteJob(job);
		}
		return 0;
	}
	
	public String querySubordinateByJobId(String jobId) throws Exception {
		return gson.toJson(
				jobMapper.querySubordinateByJobId(jobId));
	}

	public String queryPersonByJobId(String jobId) throws Exception {
		return gson.toJson(
				jobMapper.queryPersonByJobId(jobId));
	}
	
	public int deleteRelationJob(String jsonData) throws Exception {
		JSONArray jsonArr = JSONArray.fromObject(jsonData);
		if(jsonArr == null || jsonArr.isEmpty()) {
			return 0;
		}
		RelationJob relJob = new RelationJob();
		int i = 0;
		for(int cnt = jsonArr.size(); i < cnt; i++) {
			JSONObject jsonObj = jsonArr.getJSONObject(i);
			relJob = (RelationJob) JSONObject.toBean(jsonObj, relJob.getClass());
			jobMapper.deleteRelationJob(relJob);
		}
		return i;
	}
	
	public int deleteRelationPerson(String jsonData) throws Exception {
		JSONArray jsonArr = JSONArray.fromObject(jsonData);
		if(jsonArr == null || jsonArr.isEmpty()) {
			return 0;
		}
		RelationPerson relPerson = new RelationPerson();
		int i = 0;
		for(int cnt = jsonArr.size(); i < cnt; i++) {
			JSONObject jsonObj = jsonArr.getJSONObject(i);
			relPerson = (RelationPerson) JSONObject.toBean(jsonObj, relPerson.getClass());
			jobMapper.deleteRelationPerson(relPerson);
		}
		return i;
	}
	
	public String getSelectedPersonList(RelationPerson relPerson) throws Exception {
		DataStoreTemplate dst = new DataStoreTemplate();
		dst.setList(jobMapper.getSelectedPersonList(relPerson));
		dst.setTotal(jobMapper.getSelectedPersonCount(relPerson));
		return gson.toJson(dst);
	}
	
	public int relationPersonController(String jsonData) throws Exception {
		JSONArray jsonArr = JSONArray.fromObject(jsonData);
		if(jsonArr == null || jsonArr.isEmpty()) {
			return 0;
		}
		RelationPerson relPerson = new RelationPerson();
		int i = 0;
		for(int cnt = jsonArr.size(); i < cnt; i++) {
			JSONObject jsonObj = jsonArr.getJSONObject(i);
			relPerson = (RelationPerson) JSONObject.toBean(jsonObj, relPerson.getClass());
			if(relPerson.getChecked().equals("true")) {
				// 当不存在时才添加新数据，防止违反唯一约束
				if(jobMapper.matchAddRelationPerson(relPerson) == 0) {
					jobMapper.insertRelationPerson(relPerson);
				}
			} else {
				jobMapper.deleteRelationPerson(relPerson);
			}
		}
		return i;
	}
	
	public String getOptionalJob(String jobId) throws Exception {
		/*
		List<Job> list = jobMapper.getOptionalJob(jobId);
		int size = list.size();
		String jobStr[][] = new String[size][2];
		for(int i = 0; i < size; i++) {
			jobStr[i][0] = list.get(i).getId();
			jobStr[i][1] = list.get(i).getJobName();
		}
		*/
		return gson.toJson(jobMapper.getOptionalJob(jobId));
	}
	
	public String getSelectedJob(String jobId) throws Exception {
		/*
		List<Job> list = jobMapper.getSelectedJob(jobId);
		int size = list.size();
		String jobStr[][] = new String[size][2];
		for(int i = 0; i < size; i++) {
			jobStr[i][0] = list.get(i).getId();
			jobStr[i][1] = list.get(i).getJobName();
		}
		*/
		return gson.toJson(jobMapper.getSelectedJob(jobId));
	}
	
	public int relationJobController(String jsonData) throws Exception {
		JSONArray jsonArr = JSONArray.fromObject(jsonData);
		int i = 0;
		RelationJob relJob = new RelationJob();
		for(int cnt = jsonArr.size(); i < cnt; i++) {
			JSONObject jsonObj = jsonArr.getJSONObject(i);
			relJob = (RelationJob) JSONObject.toBean(jsonObj, relJob.getClass());
			if(relJob.getIsSelected().equals("true")) {
				// 不存在时才添加新数据，防止违反唯一约束
				if(jobMapper.matchAddRelationJob(relJob) == 0) {
					jobMapper.insertRelationJob(relJob);
				}
			} else {
				jobMapper.deleteRelationJobItemSelect(relJob);
			}
		}
		return i;
	}
	
	/**
	 * 获取 jobMapper
	 * @return 返回 jobMapper8
	 */
	public IJobMapper getJobMapper() {
		return jobMapper;
	}

	/**
	 * 设置 jobMapper
	 * @param 对jobMapper进行赋值
	 */
	public void setJobMapper(IJobMapper jobMapper) {
		this.jobMapper = jobMapper;
	}
	
	
}
